package com.zhuhjay.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuhjay.ruiji.common.R;
import com.zhuhjay.ruiji.domain.Dish;
import com.zhuhjay.ruiji.domain.DishFlavor;
import com.zhuhjay.ruiji.domain.dto.DishDto;
import com.zhuhjay.ruiji.service.ICategoryService;
import com.zhuhjay.ruiji.service.IDishFlavorService;
import com.zhuhjay.ruiji.service.IDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZhuHJay
 * @date 2022/5/16 17:56
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    private final IDishService dishService;
    private final IDishFlavorService dishFlavorService;
    private final ICategoryService categoryService;
    public DishController(IDishService dishService, IDishFlavorService dishFlavorService, ICategoryService categoryService) {
        this.dishService = dishService;
        this.dishFlavorService = dishFlavorService;
        this.categoryService = categoryService;
    }

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveDishAndFlavor(dishDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page<DishDto>> page(int page, int pageSize, String name){
        // 查询所有的菜品
        Page<Dish> dishPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name)
                .orderByDesc(Dish::getUpdateTime);
        dishService.page(dishPage, queryWrapper);
        // 以上查询的数据不够, 还需要有`菜品分类`的相关数据
        Page<DishDto> dishDtoPage = new Page<>();
        // 将属性拷贝, 然后忽略拷贝数据列表后续自行处理
        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");

        // 数据列表进行处理: 将菜品分类查询出来然后放到新的dto对象中
        List<Dish> records = dishPage.getRecords();
        List<DishDto> dishDtoList = records.stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            // 修改菜品分类的名称
            dishDto.setCategoryName(categoryService.getById(item.getCategoryId()).getName());
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(dishDtoList);

        return R.success(dishDtoPage);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateDishAndFlavor(dishDto);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<DishDto> query(@PathVariable("id") Long id){
        DishDto dishDto = dishService.queryDishAndFlavor(id);
        return R.success(dishDto);
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                // 查询起售状态的
                .eq(Dish::getStatus, 1)
                .eq(dish.getName() != null, Dish::getName, dish.getName())
                .eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId())
                .orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<DishDto> dishDtoList = dishService.list(queryWrapper).stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            // 查询该菜品的所有口味信息
            dishDto.setFlavors(dishFlavorService.list(new LambdaQueryWrapper<DishFlavor>()
                    .eq(DishFlavor::getDishId, item.getId())));
            return dishDto;
        }).collect(Collectors.toList());

        return R.success(dishDtoList);
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        // TODO 批量/单删除菜品, 需要考虑是否被套餐使用
        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public R<String> enableStatus(@PathVariable("status") Integer status, @RequestParam List<Long> ids){
        // TODO 批量/单修改菜品的状态
        return R.success("修改成功");
    }

}
